import numpy as np
def swapIK(V):
    I,J,K = V.shape
    V_transposed = np.transpose(V, (1, 0, 2))
    V_reshaped = V_transposed.reshape(K,J,I)
    return V_reshaped
def read_dat(fileName,dx,dy,dz):
    with open(fileName,"r") as f:
        for i in np.arange(10):
            strLine = f.readline()
            # print(strLine)
            if(strLine.find('ZONETYPE=Ordered')>=0):
                parts = strLine.split(',')             # 分割为 ['I=36', ' J=63', ' K=1', ' ZONETYPE=Ordered']
                I = int(parts[0].split('=')[1])  # 提取63
                J = int(parts[1].split('=')[1])  # 提取36
                K = int(parts[2].split('=')[1])  # 提取1
        data = np.loadtxt(f,dtype=np.float32)
        f.close()
    index_I = data[:,0].reshape((I,J,K)).astype(np.int32)
    index_J = data[:,1].reshape((I,J,K)).astype(np.int32)
    index_K = data[:,2].reshape((I,J,K)).astype(np.int32)
    V = data[:,3].reshape((I,J,K))

    '''将下标变换为0开始'''
    min_I = np.min(index_I)
    min_J = np.min(index_J)
    min_K = np.min(index_K)

    #print(min_I,min_J,min_K)
    index_I = index_I-min_I
    index_J = index_J-min_J
    index_K = index_K-min_K


    Nx = I
    Ny = J
    Nz = K

    x0 = dx*min_I
    y0 = dy*min_J
    z0 = dz*min_K
    
    return (Nx,Ny,Nz,x0,y0,z0,swapIK(V))
def write_dat(fileName,Nxyz,dxyz0 ,V):
    Nx,Ny,Nz = Nxyz
    dx,dy,dz,x0,y0,z0 = dxyz0
    index_K,index_J,index_I = np.meshgrid(int(z0/dz)+np.arange(Nz),int(y0/dy)+np.arange(Ny),int(x0/dx)+np.arange(Nx))
    data = np.zeros((Nx*Ny*Nz,4))
    data[:,0] = swapIK(index_I).ravel()+1
    data[:,1] = swapIK(index_J).ravel()+1
    data[:,2] = swapIK(index_K).ravel()+1
    data[:,3] = swapIK(V).ravel()
    with open(fileName,"w") as f:
        strHead = f'''TITLE     = "Skirt"
VARIABLES = "X(M)"
"Y(M)"
"Z(M)"
"P(N)"
ZONE T="Carpet"
 STRANDID=0, SOLUTIONTIME=0
 I={Nx}, J={Ny}, K={Nz}, ZONETYPE=Ordered
 DATAPACKING=point
 DT=(SINGLE SINGLE SINGLE SINGLE )
'''
        f.write(strHead)
        np.savetxt(f,data,fmt='%d	%d	%d	%16.13f')
def write_bin(fileName,Nx,Ny,Nz,dx,dy,dz,x0,y0,z0,V):
    #将内容写进二进制文件
    with open(fileName,'wb') as f:
        f.write(np.array([Nx,Ny,Nz],dtype=np.int32))
        f.write(np.array([dx,dy,dz,x0,y0,z0],dtype=np.float32))
        #V.tofile(f)
        f.write(V.ravel())
def read_bin(fileName):
    with open(fileName,'rb') as f:
        Nxyz = np.frombuffer(f.read(12), dtype=np.int32)
        dxyz0 = np.frombuffer(f.read(24), dtype=np.float32)
        Nx,Ny,Nz = Nxyz
        V = np.frombuffer(f.read(4*Nx*Ny*Nz),dtype=np.float32)
    return Nxyz,dxyz0,V.reshape((Nz,Ny,Nx))
# dx = 0.004
# dy = 0.004
# dz = 0.004
# Nx,Ny,Nz,x0,y0,z0,V = read_dat("map2tec.dat",dx,dy,dz)
# print(V[0,0,:])
# #Nx,Ny,Nz,dx,dy,dz,x0,y0,z0,data
# #{data[i][j][k]}  4float
# write_bin('aa.bvis',Nx,Ny,Nz,dx,dy,dz,x0,y0,z0,V)

# Nxyz,dxyz0 ,V= read_bin('aa.bvis')
# print(V[0,0,:])

# write_dat('New.dat',Nxyz,dxyz0 ,V)
class ElaspedTime:
    def __init__(self):
        import time
        self.global_start_time = time.time()
    def elapsed(self):
        import time
        tmp_time = time.time()
        strLogs = f'共计耗时：{tmp_time-self.global_start_time}s'
        print(strLogs,flush=True)